<script lang="ts" setup>
import axios from 'axios';
import { ref,onMounted } from 'vue';
import { useRoute } from 'vue-router';
import { useRouter } from 'vue-router';
import {useUserStore} from "@/store/user"

const userStore=useUserStore()
let router=useRouter()

let form=ref({
    username:"",
    password:""
})
function onSubmit(){
    console.log(form.value)
    axios.post("http://localhost:8080/api/auth/login",form.value).then(res=>{
        console.log(res.data)
        localStorage.setItem("token",res.data.data.token)
        localStorage.setItem("username",res.data.data.username)
        localStorage.setItem("userid",res.data.data.userid)
        router.replace("/books")
    }).then(async ()=>{
      const res=await userStore.fetchMenus()
    }).catch(error=>{
      console.log(error.response.data.message)
    })
}
function onEdit() {
  router.push({
    path: "/edit"
  })

}
</script>

<template>
    <h1>登录</h1>

  <div class="login-container">
  <el-form :model="form" label-width="auto" style="max-width: 600px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" />
      </el-form-item>

      <el-form-item label="密码">
        <el-input v-model="form.password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button type="primary" @click="onEdit">注册</el-button>

      </el-form-item>
    </el-form>
  </div>

</template>

<style scoped>
.login-container {
  display: flex;
  /*设置 Flex 容器的主轴方向（子元素的排列方向）。 子元素垂直排列
  flex-direction: column;
  水平方向每个子元素居中
  align-items: center;*/
  /*控制子元素在主轴方向上的对齐方式 垂直方向整体居中*/
  justify-content: center;
  padding: 20px;
}
</style>